package socialplaytime;

import java.awt.*;
import  sun.audio.*;    //import the sun.audio package
import  java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;
import javax.swing.Timer;
import java.util.*;

@SuppressWarnings({ "serial", "unused" })
public class NextPagePanel extends JPanel {

	private JButton push, pushBack;
	String test= "This is a test";
	String textFile, text;
	String s2= ".page";
	String imgtxt= ".IMGTXT";
	String txt = ".txt";
	String s3= ".jpg";
	private JLabel picture;
	int timerCounter = 0;
	Font font = new Font(Font.SERIF, Font.BOLD, 20);
	Font bigger = font.deriveFont(22);
	Scanner textScan, prevScan;

	JFrame frame;
	private Timer timer;
	private final int DELAY = 15000; // 15 second delay
	private String s1;
	private int i=0;

	PlayMenuGUI menu;

	public NextPagePanel (final JFrame frame, String s, final PlayMenuGUI menu) {
		this.frame = frame;
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		WindowListener windowListener = new WindowListener() {
			public void windowClosing(WindowEvent w) {
				frame.setVisible(false);
				menu.getFrame().setVisible(true);
				timer.stop();
			}

			@Override
			public void windowActivated(WindowEvent arg0) {
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
			}
		};
		frame.addWindowListener(windowListener);
		this.menu = menu;
		push = new JButton ("Next");
		pushBack = new JButton ("Previous");
		picture = new JLabel();
		System.out.println(s);
		s1=s;
		try {
			textScan = new Scanner(new FileReader(s1 + imgtxt + i + txt))	;
		} catch (FileNotFoundException e) {
		}
		text = textScan.nextLine();
		picture.setIcon(new ImageIcon(s1+s2+i+s3)); //change back to this after testing
		picture.setText(text);
		picture.setFont(bigger);
		picture.setVerticalTextPosition(JLabel.BOTTOM);
		picture.setHorizontalTextPosition(JLabel.CENTER);

		push.addActionListener(new NextListener());
		pushBack.addActionListener(new PrevListener());

		JPanel south = new JPanel(new FlowLayout());
		south.add(pushBack);
		south.add(push);
		timer = new Timer(DELAY, new TimerListener());
		picture.setPreferredSize(new Dimension(1300, 650));
		picture.setBorder(BorderFactory.createLineBorder (Color.blue, 2));
		setLayout(new BorderLayout());
		add(picture, BorderLayout.CENTER);
		add(south, BorderLayout.SOUTH);
		timer.start();
		if (i == 0)
			pushBack.setVisible(false);
	}

	private class PrevListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			push.setText("Next");
			if (i >= 0)
				i--;
			if (i == 0)
				pushBack.setVisible(false);
			String s = s1+s2+i+s3;
			textFile = s1 + ".IMGTXT" + i + ".txt";

			try {
				prevScan = new Scanner(new FileReader(textFile));
			}
			catch (Exception e) {
				return;
			}
			text = prevScan.nextLine();

			if (text.startsWith("﻿"))
				text = text.substring(3, text.length()-1);
			picture.setIcon(new ImageIcon(s, test));
			picture.setText(text);
			picture.setVerticalTextPosition(JLabel.BOTTOM);
			picture.setHorizontalTextPosition(JLabel.CENTER);
			picture.repaint();
		}
	}


	private class NextListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) {		
			timer.stop();			
			i++;
			if (i > 0)
				pushBack.setVisible(true);
			String s = s1+s2+i+s3;
			textFile = s1 + ".IMGTXT" + i + ".txt";
			// try to open the file
			try {
				textScan = new Scanner (new FileReader (textFile));				
			} 
			catch (Exception e) {
				// can't open it?
				frame.setVisible(false);
				menu.getFrame().setVisible(true);
				timer.stop();
			}
			text = textScan.nextLine();
			if (text.startsWith("﻿"))
				text = text.substring(3, text.length()-1);
			picture.setIcon(new ImageIcon(s, text));
			picture.setText(text);
			picture.setVerticalTextPosition(JLabel.BOTTOM);
			picture.setHorizontalTextPosition(JLabel.CENTER);
			picture.repaint();
			timer.start();
			String tempfile = s1 + ".IMGTXT" + (i + 1) + ".txt";
			try {
				Scanner temp = new Scanner (new FileReader(tempfile));
			}
			catch (FileNotFoundException e) {
				push.setText("Close");
			}
			if (push.getText() == "Close")
				timer.stop();
		}
	}

	private class TimerListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg1) {
			timerCounter++;
			if (timerCounter > 2)
				timer.stop();
			// this will import the audio file
			InputStream in;
			try {
				in = new FileInputStream("nextPageAudio.wav"); }
			catch (Exception e)
			{
				return;
			}
			AudioStream as = null;
			try {
				as = new AudioStream(in);
			} catch (IOException e) {
			}
			AudioPlayer.player.start(as);
		}
	}
}